package cn.zbzcode.hrm.service.impl;

import cn.zbzcode.hrm.doc.CourseDoc;
import cn.zbzcode.hrm.domain.Course;
import cn.zbzcode.hrm.domain.CourseDetail;
import cn.zbzcode.hrm.domain.CourseMarket;
import cn.zbzcode.hrm.dto.CourseAddDto;
import cn.zbzcode.hrm.feign.client.ESFeignClient;
import cn.zbzcode.hrm.mapper.CourseDetailMapper;
import cn.zbzcode.hrm.mapper.CourseMapper;
import cn.zbzcode.hrm.mapper.CourseMarketMapper;
import cn.zbzcode.hrm.query.CourseQuery;
import cn.zbzcode.hrm.service.ICourseService;
import cn.zbzcode.hrm.utils.AjaxResult;
import cn.zbzcode.hrm.utils.PageList;
import cn.zbzcode.hrm.utils.UserContext;
import cn.zbzcode.hrm.vo.LoginInfo;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zbz
 * @since 2020-04-18
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
    @Autowired
    private ESFeignClient esFeignClient ;

    @Autowired
    private CourseDetailMapper courseDetailMapper  ;

    @Autowired
    private CourseMarketMapper courseMarketMapper ;


    @Override
    public void saveOrUpdate(CourseAddDto dto) {

        //拿到dto中的值
        Course course = dto.getCourse();
        CourseMarket courseMarket = dto.getCourseMarket();
        CourseDetail courseDetail = dto.getCourseDetail();

        //参数合法性判断等。。
        if(!StringUtils.hasLength(course.getName())){
            throw new RuntimeException("课程名字不能为空") ;
        }

        //保存课程
        course.setStatus(Course.STATUS_OFFLINE);
        LoginInfo loginInfo = UserContext.getLoginInfo();
        course.setUserId(loginInfo.getUserId());
        course.setUserName(loginInfo.getUserName());
        course.setTenantId(loginInfo.getTenantId());
        course.setTenantName(loginInfo.getTenantName());
        baseMapper.insert(course);  //可以直接插入

        //课程详情保存
        courseDetail.setId(course.getId()); //id一对一
        courseDetailMapper.insert(courseDetail);

        //课程营销保存
        courseMarket.setValid(CourseMarket.STATE_VALID);
        courseMarket.setId(course.getId());
        courseMarketMapper.insert(courseMarket) ;
    }

    //发布课程
    @Override
    @Transactional
    public void onLineCourse(Long id) {
        //1.根据课程ID获取课程对象
        Course course = baseMapper.selectById(id);
        //2.判断课程对象不为空
        if(course == null){
            throw new RuntimeException("无效的课程");
        }
        //3.判断课程状态必须是“未发布”
        if(course.getStatus() != Course.STATUS_OFFLINE){
            throw new RuntimeException("无效的课程状态");
        }
        //4.修改课程状态为“已发布”
        course.setStatus(Course.STATUS_ONLINE);
        int result = baseMapper.updateById(course);
        if(result == 1){
            //课程营销
            CourseMarket courseMarket = courseMarketMapper.selectById(id);
            //课程详情
            CourseDetail courseDetail = courseDetailMapper.selectById(id);
            //5.把课程存储到ES中
            CourseDoc doc = new CourseDoc() ;
            //5.1.CourseDoc应该有哪些字段
            //5.2.怎么把这些字段设置到CourseDoc中: 工具类，属性值的拷贝
            BeanUtils.copyProperties(courseMarket,doc);
            BeanUtils.copyProperties(courseDetail,doc);
            BeanUtils.copyProperties(course,doc);
            doc.setOnlineTime(new Date());
            doc.setCharge(courseMarket.getCharge());
            AjaxResult ajaxResult = esFeignClient.onLineCourse(doc);
            if(!ajaxResult.isSuccess()){
                throw new RuntimeException("课程上线失败");
            }
        }else{
            throw new RuntimeException("课程状态修改失败");
        }
    }

    //课程下线
    @Override
    @Transactional
    public void offLineCourse(Long id) {
        //1.根据课程ID获取课程对象
        Course course = baseMapper.selectById(id);
        //2.判断课程对象不为空
        if(course == null){
            throw new RuntimeException("无效的课程");
        }
        //3.判断课程是否已经下线
        if(course.getStatus() == Course.STATUS_OFFLINE  ){
            throw new RuntimeException("该课程已经下线");
        }

        //4.修改课程状态为“未发布”
        course.setStatus(Course.STATUS_OFFLINE);
        int result = baseMapper.updateById(course);
        if(result == 1){
            AjaxResult ajaxResult = esFeignClient.offLineCourse(id);
            if(!ajaxResult.isSuccess()){
                throw new RuntimeException("课程下线失败");
            }
        }else {
            throw new RuntimeException("课程状态修改失败");
        }


    }

    @Override
    public PageList<CourseDoc> queryCourses(CourseQuery courseQuery) {
        return esFeignClient.searchCourse(courseQuery);
    }
}
